/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.activity.helper;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.uml2.uml.Activity;
import org.eclipse.uml2.uml.ActivityNode;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Abstract class used to managed {@link ActivityNode}
 *
 *
 * @author Gilles Cannenterre
 * @since 23 mars 07
 */
public abstract class NodeActivityNodeHelper extends NodeHelper {

	/** The Constant ACTION_PRECONDITION_NAME. */
	public static final String ACTION_PRECONDITION_NAME = "ActionPrecondition";
	
	/** The Constant ACTION_POSTCONDITION_NAME. */
	public static final String ACTION_POSTCONDITION_NAME = "ActionPostcondition";

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#addUMLElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	@Override
	public boolean addUMLElement(Element element, Element umlParent) {
		if (umlParent instanceof Activity) {
			return ((Activity)umlParent).getNodes().add((ActivityNode)element);
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#canCreateGraphElement(com.cea.papyrus.core.diagraminterchange2.di2.GraphElement)
	 */
	@Override
	public boolean canCreateGraphElement(GraphElement parent){
		if (parent instanceof Diagram) {
			return false;
		} else if (parent.getSemanticModel()!=null){
			Element parentElt= ((Uml1SemanticModelBridge)parent.getSemanticModel()).getElement();
			if(parentElt!=null){
				return canCreateUMLElement(parentElt);
			}
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#canCreateUMLElement(org.eclipse.uml2.uml.Element)
	 */
	@Override
	public boolean canCreateUMLElement(Element umlParent) {
		if (umlParent instanceof Activity) {
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#removeUMLElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	@Override
	public boolean removeUMLElement(Element element, Element umlParent) {
		if (umlParent instanceof Activity) {
			return ((Activity)umlParent).getNodes().remove(element);
		}
		return false;
	}

	@Override
	public Element createUMLElement(String name, Element umlParent){
		Element newAction = null;
		if (umlParent instanceof Activity) {
			newAction = ((Activity) umlParent).createNode(name, getNodeType() );		
		}
		
		return newAction;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#getGraphNodeSize()
	 */
	@Override
	public Dimension getGraphNodeSize() {
		return new Dimension(150,50);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#getNewUMLElementName(org.eclipse.uml2.uml.Element)
	 */
	@Override
	public String getNewUMLElementName(Element umlParent) {
		String name = ""; //$NON-NLS-1$
	
		// i <10000: avoid infinite loops
		for(int i=0; i< 10001; i++ ) {
			name = getBaseString()+i;
			if (umlParent instanceof Activity){
				if(null == ((Activity)umlParent).getNode(name)) {
					return name;
				}
			}
		}
		return getBaseString()+"X"; //$NON-NLS-1$
	}
	
	/**
	 * This method must be implemented in Helper class that inherit from current
	 * and are dedicated to specific Node or Action management.
	 * This method is supposed to be used in createUMLElement.
	 * @return the {@link EClass} type of the node to create
	 */
	protected abstract EClass getNodeType();
}